﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConceptCorporateAdmin.Gui
{
    public partial class ExtendedTreeView : TreeView
    {
        ImageList icons = new ImageList();

        public ExtendedTreeView()
        {

            try
            {
                icons.Images.Add("folder", Image.FromFile("images/icons/folder.jpg"));
                icons.Images.Add("act"   , Image.FromFile("images/icons/act.png"));
                icons.Images.Add("folder", Image.FromFile("images/icons/folder.jpg"));
            }
            catch (Exception ex)
            {
                Console.WriteLine( ex.Message + ex.StackTrace );
            }
            this.ImageList = icons;
        }

        public void RefreshLista()
        {
            this.Nodes.Clear();
            DbCategorii.Instance.RefreshTable();
            addChildrens(DbCategorii.Instance.Table, this.Nodes, -1);
        }

        private void addChildrens(DataTable BaseDt, TreeNodeCollection nodes, int id_father)
        {
            DataRow[] rows = BaseDt.Select("IdFather='" + id_father + "'");
            foreach (DataRow row in rows)
            {
                DbCategorie cat     = new DbCategorie(row);
                ExTreeNode node     = new ExTreeNode(cat.Nume, TipuriNoduri.Categorie, cat);
                nodes.Add(node);
                addChildrens(BaseDt, node.Nodes, cat.Id);
            }
            //adauga actele fiecarei categorii
            DataTable dt_acte = DbConnection.Instance.Query("SELECT * FROM \"acte_normative\" WHERE \"IdCategorie\"='" + id_father + "'");
            foreach (DataRow row in dt_acte.Rows)
            {
                DbActNormativ act = new DbActNormativ(row);
                ExTreeNode node   = new ExTreeNode(act.Nume, TipuriNoduri.ActeNormative, act);
                nodes.Add(node);
            }
        }

        public ExTreeNode SelectedExNode
        {
            get
            {
                ExTreeNode ret = null;
                if (SelectedNode != null)
                    ret = (ExTreeNode)SelectedNode;
               
                return ret;
            }
        }

        public void RemoveSelectedNode()
        {
            if (this.SelectedExNode != null)
            {
                DialogResult r =  MessageBox.Show("Sunteti sigur?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (r == DialogResult.Yes)
                {
                    //preia obiectul si sterge-l
                    DbObject o = (DbObject)SelectedExNode.InnerObject;
                    o.Delete();
                    //sterge si nodul din treeview
                    this.SelectedExNode.Remove();
                }
            }
        }

        public void AddCategorie(String nume)
        {
            DbCategorie c   = (DbCategorie)this.SelectedExNode.InnerObject;
            DbCategorie cat = new DbCategorie();
            cat.IdFather    = c.Id;
            cat.Nume        = nume;
            cat.CreateNew();
            //adauga manual trenode-ul
            ExTreeNode node = new ExTreeNode(cat.Nume , TipuriNoduri.Categorie, cat);
            this.SelectedExNode.Nodes.Add(node);
        }

        public void AddCategorieRoot(String nume)
        {
            DbCategorie c   = (DbCategorie)this.SelectedExNode.InnerObject;
            DbCategorie cat = new DbCategorie();
            cat.IdFather    = -1;
            cat.Nume        = nume;
            cat.CreateNew();
            //adauga manual trenode-ul
            ExTreeNode node = new ExTreeNode(cat.Nume , TipuriNoduri.Categorie, cat);
            this.Nodes.Add(node);
        }



    }
}
